package com.lindroy.androidplayer.ui.adapter

import android.content.Context
import android.widget.FrameLayout
import android.widget.TextView
import com.kunminx.linkage.adapter.viewholder.LinkagePrimaryViewHolder
import com.kunminx.linkage.adapter.viewholder.LinkageSecondaryHeaderViewHolder
import com.kunminx.linkage.adapter.viewholder.LinkageSecondaryViewHolder
import com.kunminx.linkage.bean.BaseGroupedItem
import com.kunminx.linkage.contract.ILinkagePrimaryAdapterConfig
import com.kunminx.linkage.contract.ILinkageSecondaryAdapterConfig
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.logic.model.bean.NavigationIndexGroupItemBean
import com.lindroy.androidplayer.utils.no
import com.lindroy.androidplayer.utils.primaryColor
import com.lindroy.androidplayer.utils.yes
import com.lindroy.lib_utils.extensions.getResColor

/**
 * @author Lin
 * @date 2022/1/27
 * @function
 */
class NavigationIndexPrimaryAdapterConfig : ILinkagePrimaryAdapterConfig {

    private lateinit var context: Context

    override fun setContext(context: Context) {
        this.context = context
    }

    override fun getLayoutId(): Int = R.layout.navigation_index_primary_item

    override fun getGroupTitleViewId(): Int = R.id.tvGroup

    override fun getRootViewId(): Int = R.id.layout_nav_group

    override fun onBindViewHolder(
        holder: LinkagePrimaryViewHolder?,
        selected: Boolean,
        title: String?
    ) {
        (holder?.groupTitle as? TextView)?.also {
            it.text = title.orEmpty()
            selected.yes {
                it.setTextColor(getResColor(R.color.text_white))
                it.setBackgroundColor(context.primaryColor)
            }.no {
                it.setTextColor(getResColor(R.color.text_gray))
                it.setBackgroundResource(R.color.white)
            }
        }
    }
}

private typealias OnNavItemClickListener = (item: NavigationIndexGroupItemBean.ItemInfo) -> Unit

class NavigationIndexSecondaryAdapterConfig :
    ILinkageSecondaryAdapterConfig<NavigationIndexGroupItemBean.ItemInfo> {

    private lateinit var context: Context

    private var listener: OnNavItemClickListener? = null

    override fun setContext(context: Context) {
        this.context = context
    }

    override fun getLinearLayoutId() = R.layout.navigation_index_secondary_item

    override fun getHeaderLayoutId() = R.layout.navigation_index_group_item

    override fun getFooterLayoutId() = R.layout.navigation_index_footer

    override fun getHeaderTextViewId() = R.id.tvGroup

    override fun onBindViewHolder(
        holder: LinkageSecondaryViewHolder?,
        item: BaseGroupedItem<NavigationIndexGroupItemBean.ItemInfo>
    ) {
        holder?.apply {
            getView<TextView>(R.id.tvItem)?.text = item.info.itemName
            getView<FrameLayout>(R.id.flNavIndex).setOnClickListener {
                listener?.invoke(item.info)
            }
        }
    }

    override fun onBindHeaderViewHolder(
        holder: LinkageSecondaryHeaderViewHolder?,
        item: BaseGroupedItem<NavigationIndexGroupItemBean.ItemInfo>
    ) {
        holder?.getView<TextView>(R.id.tvGroup)?.text = item.header
    }

    fun setOnItemClickListener(listener: OnNavItemClickListener) {
        this.listener = listener
    }

}

